package com.c1202m.util;

import com.c1202m.gui.CustomMessageDialog;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

public class KiemTraDuLieu {

    public static boolean isEmpty(String text) {
        return text.trim().length() == 0;
    }

    public static boolean kiemTraTruongTrong(JTextComponent[] component) {
        if (component != null) {
            for (int i = 0; i < component.length; i++) {
                component[i].setBackground(Color.white);
                if (isEmpty(component[i].getText())) {
                    showMessage("Không chấp nhận giá trị trống");
                    component[i].setBackground(new Color(250, 255, 168));
                    component[i].requestFocus();
                    return false;
                }
            }
        }
        return true;
    }

    public static void showMessage(String message) {
        new CustomMessageDialog(null, true, "Validation Message", message, CustomMessageDialog.MESSAGE);
    }

    public static void setBlankFields(JTextComponent[] tcp) {
        for (int i = 0; i < tcp.length; i++) {
            tcp[i].setText("");
        }
    }

    public static boolean kiemTraSoDienThoai(JTextField txtPhone) {
        if (!txtPhone.getText().matches("[\\d]{8,15}")) {
            showMessage("Điện thoại không hợp lệ");
            txtPhone.setBackground(new Color(250, 255, 168));
            txtPhone.requestFocus();
            return false;
        }
        return true;
    }

    public static boolean kiemTraEmail(JTextField txtEmail) {
        if (!txtEmail.getText().matches("^(([\\-\\w]+)\\.?)+@(([\\-\\w]+)\\.?)+\\.[a-zA-Z]{2,4}$")) {
            showMessage("Email không hợp lệ");
            txtEmail.setBackground(new Color(250, 255, 168));
            txtEmail.requestFocus();
            return false;
        }
        return true;
    }
    
    public static boolean kiemTraHoTen(JTextField txtHoTen) {
        if (!txtHoTen.getText().matches("^[A-z].*[\\S]$")) {
            showMessage("Giá trị không hợp lệ");
            txtHoTen.setBackground(new Color(250, 255, 168));
            txtHoTen.requestFocus();
            return false;
        }
        return true;
    }
    
    public static boolean kiemTraSo(JTextField txtHoTen) {
        if (!txtHoTen.getText().matches("^[1-9][\\d]*$")) {
            showMessage("Giá trị không hợp lệ");
            txtHoTen.setBackground(new Color(250, 255, 168));
            txtHoTen.requestFocus();
            return false;
        }
        return true;
    }
    
    public static boolean kiemTraSoCMND(JTextField txtHoTen) {
        if (!txtHoTen.getText().matches("^[\\d][\\d]{8,9}$")) {
            showMessage("Giá trị không hợp lệ");
            txtHoTen.setBackground(new Color(250, 255, 168));
            txtHoTen.requestFocus();
            return false;
        }
        return true;
    }
}
